package com.hursing.tractor.model;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import com.hursing.tractor.baseview.UIView;
import com.hursing.tractor.view.UIHelper;

public final class GameData {
	
	private int mFlipperPosition = Player.POSITION_UNKNOWN;
	private int mScore;
	private int mHandPosition = Player.POSITION_BOTTOM;
	private Player[] mPlayers = new Player[4];
	private final LinkedList<Card> mFlippedCards = new LinkedList<Card>();
	private final LinkedList<Card> mKitty = new LinkedList<Card>();
	private final ArrayList<LinkedList<Card>> mLastRoundCards = new ArrayList<LinkedList<Card>>(Player.PLAYER_COUNT);
	private final ArrayList<LinkedList<Card>> mPlayedCards = new ArrayList<LinkedList<Card>>(Player.PLAYER_COUNT);
	private boolean mShowingPrevious;

	private int mDeckCount = 2;
	private int mMasterTB = Card.NUMBER_2;
	private int mMasterLR = Card.NUMBER_2;
	private int mHousePosition = Player.POSITION_UNKNOWN;
	
	private static GameData mInstance = new GameData();
	public static GameData instance() {
		return mInstance;
	}
	private GameData() {
		int count = Player.PLAYER_COUNT;
		while (count-- > 0) {
			mPlayedCards.add(new LinkedList<Card>());
			mLastRoundCards.add(new LinkedList<Card>());
		}
		mPlayers[Player.POSITION_BOTTOM] = new HumanPlayer(Player.POSITION_BOTTOM);
		mPlayers[Player.POSITION_LEFT] = new ComputerPlayer(Player.POSITION_LEFT);
		mPlayers[Player.POSITION_TOP] = new ComputerPlayer(Player.POSITION_TOP);
		mPlayers[Player.POSITION_RIGHT] = new ComputerPlayer(Player.POSITION_RIGHT);
	}
	
	public List<Card> kittyCards() {
		return mKitty;
	}
	
	public ArrayList<LinkedList<Card>> playedCards() {
		return mPlayedCards;
	}
	
	public GameData playedCards(LinkedList<Card> cards, int position) {
		mPlayedCards.get(position).clear();
		if (cards != null) {
			mPlayedCards.get(position).addAll(cards);
		}
		UIView.sViews[UIView.VIEW_BOTTOM_CARD + position].toCardView().cards(cards);
		return this;
	}
	
	public GameData clearPlayedCards() {
		for (int i = 0; i < Player.PLAYER_COUNT; i++) {
			playedCards(null, i);
		}
		return this;
	}
	
	public ArrayList<LinkedList<Card>> lastRoundCards() {
		return mLastRoundCards;
	}
	
	public boolean showingPrevious() {
		return mShowingPrevious;
	}
	
	public GameData showingPrevious(boolean showing) {
		mShowingPrevious = showing;
		return this;
	}
	
	public LinkedList<Card> flippedCards() {
		return mFlippedCards;
	}
	
	public Player player(int position) {
		return mPlayers[position];
	}
	
	public GameData players(Player[] players) {
		mPlayers = players;
		return this;
	}
	
	public int flipperPosition() {
		return mFlipperPosition;
	}
	
	public GameData flip(Player player, List<Card> flippedCards) {
		if (mFlipperPosition != Player.POSITION_UNKNOWN) {
			UIView.sViews[UIView.VIEW_BOTTOM_CARD + mFlipperPosition].toCardView().cards(null);
		}
		mFlippedCards.clear();
		if (player == null || flippedCards == null) {
			mFlipperPosition = Player.POSITION_UNKNOWN;
			UIView.sViews[UIView.VIEW_FLIPPED_SUIT].toFlippedSuitView().flippedSuit(Card.SUIT_UNKNOWN);
		} else {
			mFlipperPosition = player.position();
			mFlippedCards.addAll(flippedCards);
			UIView.sViews[UIView.VIEW_BOTTOM_CARD + mFlipperPosition].toCardView().cards(flippedCards);
			UIView.sViews[UIView.VIEW_FLIPPED_SUIT].toFlippedSuitView().flippedSuit(flippedCards.get(0).suit()).flippedCount(flippedCards.size());
		}
		UIView.sViews[UIView.VIEW_CURRENT_FLIPPER_LABEL].text(UIHelper.positionToText(mFlipperPosition));
		return this;
	}
	
	public int score() {
		return mScore;
	}
	
	public GameData score(int score) {
		mScore = score;
		UIView.sViews[UIView.VIEW_CURRENT_SCORE_LABEL].text(String.valueOf(mScore));
		return this;
	}
	
	public GameData scoreplus(int plus) {
		return score(mScore + plus);
	}
	
	public int handPosition() {
		return mHandPosition;
	}
	
	public GameData handPosition(int position) {
		if (mHandPosition != position) {
			mHandPosition = position;
			UIView.sViews[UIView.VIEW_HANDY_CARD].toHandyCardView().cards(mPlayers[mHandPosition].allHandyCards());
			for (int i = 0; i < Card.SUIT_COUNT; i++) {
				UIView.sViews[UIView.VIEW_FLIP_DIAMOND_BUTTON + i].enabled(mPlayers[mHandPosition].flipAbility(i));
			}
		}
		return this;
	}
	
	public int deckCount() {
		return mDeckCount;
	}
	
	public GameData deckCount(int count) {
		mDeckCount = count;
		return this;
	}
	
	public int masterTB() {
		return mMasterTB;
	}
	
	public GameData masterTB(int master) {
		mMasterTB = master;
		updateCurrentMasterLabel();
		return this;
	}
	
	public int masterLR() {
		return mMasterLR;
	}
	
	public GameData masterLR(int master) {
		mMasterLR = master;
		updateCurrentMasterLabel();
		return this;
	}
	
	public int housePosition() {
		return mHousePosition;
	}
	
	public GameData housePosition(int position) {
		mHousePosition = position;
		UIView.sViews[UIView.VIEW_CURRENT_HOUSE_LABEL].text(UIHelper.positionToText(mHousePosition));
		updateCurrentMasterLabel();
		return this;
	}

	// 便捷函数
	public int houseSide() {
		return mPlayers[mHousePosition].side();
	}
	
	public int getCurrentMaster() {
		return houseSide() == Player.SIDE_LEFT_RIGHT ? GameData.instance().masterLR() : GameData.instance().masterTB();
	}
	
	public int getFlippedSuit() {
		int suit = mFlippedCards.size() == 0 ? Card.SUIT_UNKNOWN : mFlippedCards.getFirst().suit();
		return suit;
	}
	
	public boolean isGettingMark(int position) {
		return (GameData.instance().housePosition() % 2) != (position % 2);
	}
	
	public Player handPositionPlayer() {
		return mPlayers[mHandPosition];
	}
	
	public Player housePositionPlayer() {
		return mPlayers[mHousePosition];
	}
	
	private void updateCurrentMasterLabel() {
		if (mHousePosition == Player.POSITION_UNKNOWN) {
			return;
		}
		int side = mPlayers[mHousePosition].side();
		String tb = UIHelper.numberToText(mMasterTB);
		String lr = UIHelper.numberToText(mMasterLR);
		String text = side == Player.SIDE_LEFT_RIGHT ? lr+"："+tb : tb+"："+lr;
		UIView.sViews[UIView.VIEW_CURRENT_MASTER_LABEL].text(text);
	}
	
}
